﻿using DG.Tweening;
using Gp.Scripts.Data;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using Image = UnityEngine.UI.Image;

namespace Gp.Scripts.GUI {
    public class BuffResourceItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
        [SerializeField] private TMP_Text amount;
        [SerializeField] private Image icon;
        [SerializeField] private CanvasGroup detail;
        [SerializeField] private TMP_Text nameText;
        [SerializeField] private TMP_Text descriptionText;


        private BaseBuff _buff;

        private void Start() {
            detail.alpha = 0;
            detail.interactable = false;
            detail.blocksRaycasts = false;
        }


        public void Render(BaseBuff buff) {
            if (buff is not IResourceBuff rb) {
                Hide();
                return;
            }

            var info = buff.OnIconRender();
            _buff = buff;
            
            amount.text = rb.Level.ToString();
            nameText.text = buff.EntityConfig.ItemName;
            descriptionText.text = buff.EntityConfig.Description;
            
            
            
            icon.sprite = info.Icon;
            HideDetail();
            gameObject.SetActive(true);
        }
        
        public void Hide() {
            _buff = null;
            gameObject.SetActive(false);
        }


        private void ShowDetail() {
            if (_buff == null) return;
            detail.transform.localScale = Vector3.one * .2f;
            detail.transform.DOScale(Vector3.one, .25f);
            detail.DOFade(1, .3f);
        }

        private void HideDetail() {
            detail.alpha = 0;
        }
        
        
        public void OnPointerEnter(PointerEventData eventData) {
            ShowDetail();
        }

        public void OnPointerExit(PointerEventData eventData) {
            HideDetail();
        }
    }
}